#ifndef FFMPEG_H
#define FFMPEG_H

#include <QMainWindow>
#include <QMutex>
#include <QDateTime>
#include <QFile>
#include <QThread>
#include <QDebug>
#include <stdio.h>
#include <string>
#include <iostream>
#include <chrono>
#include <vector>
//引入ffmpeg头文件
extern "C" {
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/frame.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#include "libavutil/ffversion.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#include "libavutil/hwcontext.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
}


class ffmpeg : public QThread
{
    Q_OBJECT
public:
    explicit ffmpeg(QWidget *parent = NULL);
    ~ffmpeg();
    char *outputFilename;
    char *inputFilename;
    struct timeval stamp;
    int start_time;
    
protected:
    void run();
signals:
    //收到图片信号
    void receiveImage(const QImage &image);

private:

    AVFormatContext *fmtCtx = NULL;;
    int framIndex;
    uint8_t *buffer;                    //存储解码后图片buffer
    AVFrame *avDeFrameYuv;              //解码帧对象YUV
    AVCodec *deCodec = NULL;            //解码器
	AVCodec *pCodecH264; //编码器

    AVPacket *avDePacket;               //解码包对象
    AVPacket avpkt;
    int frameFinish = 0;
    int stream_index;
    FILE* MJPEGPath;
    FILE* YUVPath;
    FILE* H264Path;
    FILE* NV12Path;
    int WIDTH,HEIGHT,FRAME;
    int videoStreamIndex;               //视频流索引
	//输入输出视频流
	AVStream *out_stream;
    AVStream *in_stream;                //输入视频流
    AVCodecContext *c= NULL;
    AVCodecContext *deCodecCtx;         //解码器上下文
    int videoWidth;                     //视频宽度
    int videoHeight;                    //视频高度
    int videoFps;
	uint8_t * outbuf;
    int outbuf_size;
    int got_packet_ptr;
    char* nv12;
    char *h264;
    char* yuv420p;
    char* RtmpULR;
    AVFormatContext *ifmt_ctx;
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ofmt_ctx = NULL;
    int waitI,rtmpisinit;
    int ptsInc=0;
    int width;
    int height;
    int Fps;

private:
    void YUV420PtoNV12(unsigned char *Src, unsigned char* Dst,int Width,int Height);
    void YUYV_to_YUV420P( char * image_in, char* image_out, int width, int height); 
    int initDecodeVideo();
    int playVideo();
    int RtmpInit(void* spspps_date, int spspps_datale);
    int GetSpsPpsFromH264(uint8_t* buf, int len);
    bool isIdrFrame2(uint8_t* buf, int len);
    bool isIdrFrame1(uint8_t* buf, int size);
    void VideoWrite(void* data, int datalen);
    void RtmpUnit(void);
};

#endif // FFMPEG_H

